﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using dotnetCHARTING;
using ChartExtents;
using System.Collections.Generic;
using System.Drawing;

public partial class main_dataAnalyze_DXLB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LineDAOs lineDAOs = LineDAOs.getInstance();
            List<String> lineNameList = lineDAOs.getLineNameList();
            for (int i = 0; i < lineNameList.Count; i++)
            {
                dropLine.Items.Add(lineNameList[i]);
            }
            TowerDAOs towerDAOs = TowerDAOs.getInstance();
            List<String> towerNameList = towerDAOs.getTowerNameList(lineNameList[0]);
            for (int j = 0; j < towerNameList.Count; j++)
            {
                dropTower.Items.Add(towerNameList[j]);
            }
            this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
            this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
            this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
            this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
            this.txtDXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线倾斜角1");
            this.txtDXLL.Text = towerDAOs.getNow(dropTower.SelectedValue, "悬点导线拉力");
            this.txtDXHC.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线弧垂");
            this.txtDXLBHD.Text = towerDAOs.getNow(dropTower.SelectedValue, "覆冰厚度");
        }
    }
    //protected void btnHistory_Click(object sender, EventArgs e)
    //{
    //    TowerDAOs towerDAOs = TowerDAOs.getInstance();
    //    this.txtWD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtSD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "湿度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtFS.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "风速", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtDXQXJ.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "导线倾斜角1", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtDXLL.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "悬点导线拉力", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtDXHC.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "导线弧垂", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    this.txtDXLBHD.Text = towerDAOs.getHistoryMax(dropTower.SelectedValue, "覆冰厚度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
    //    
    //}
    protected void dropLine_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        dropTower.Items.Clear();
        List<String> towerNameList = towerDAOs.getcorrespondingTowerNameList_DXLB(dropLine.SelectedValue);
        for (int j = 0; j < towerNameList.Count; j++)
        {
            dropTower.Items.Add(towerNameList[j]);
        }
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
        this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
        this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtDXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线倾斜角1");
        this.txtDXLL.Text = towerDAOs.getNow(dropTower.SelectedValue, "悬点导线拉力");
        this.txtDXHC.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线弧垂");
        this.txtDXLBHD.Text = towerDAOs.getNow(dropTower.SelectedValue, "覆冰厚度");
    }
    private void Drawing(int i)
    {
        Chart[] chart = { this.Chart1, this.Chart2, this.Chart3, this.Chart4, this.Chart5, this.Chart6, this.Chart7 };
        chart[i].Type = ChartType.Combo;//Horizontal;
        chart[i].Size = "600x350";
        chart[i].TempDirectory = "images";
        chart[i].Debug = true;
        chart[i].XAxis.TimeInterval = TimeInterval.Day;
        chart[i].XAxis.Scale = Scale.Time;
        
        if (i == 0)
        {
            chart[i].YAxis.Name = "覆冰厚度";
        }
        else if (i == 1)
        {
            chart[i].YAxis.Name = "温度";
        }
        else if (i == 2)
        {
            chart[i].YAxis.Name = "湿度";
        }
        else if (i == 3)
        {
            chart[i].YAxis.Name = "导线弧垂";
        }
        else if (i == 4)
        {
            chart[i].YAxis.Name = "风速";
        }
        else if (i == 5)
        {
            chart[i].YAxis.Name = "导线倾斜角";
        }
        else
        {
            chart[i].YAxis.Name = "导线拉力";
        }
        SeriesCollection mySC = GetDataSource(i);
        chart[i].DefaultSeries.Type = SeriesType.Spline;
        chart[i].TitleBox.Position = TitleBoxPosition.FullWithLegend;
        chart[i].Series.StartDate = RadDatePicker1.SelectedDate.Value.Date;
        chart[i].Series.EndDate = RadDatePicker2.SelectedDate.Value.Date;
        // chart[i].DefaultSeries.Type = SeriesTypeFinancial.Close;
        //SeriesCollection mySC = GetDataSource();
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        DataSet ds = towerDAOs.getAlarm(dropTower.SelectedValue, RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        int j = ds.Tables[0].Rows.Count;
        String[] description = new String[j];
        for (int b = 0; b < j; b++)
        {
            description[b] = ds.Tables[0].Rows[b]["act_description"].ToString();
            foreach (Element myE in mySC[0].Elements)
            {
                if (description[b].Equals("覆冰轻度告警"))
                    myE.Color = Color.Yellow;
                else if (description[b].Equals("覆冰中度告警"))
                    myE.Color = Color.Orange;
                else if (description[b].Equals("覆冰重度告警"))
                    myE.Color = Color.Red;
            }
            chart[i].XAxis.Markers.Add(new AxisMarker("", Color.FromArgb(50, Color.Red), ds.Tables[0].Rows[b]["start_time"], ds.Tables[0].Rows[b]["stop_time"]));
        }
        chart[i].SeriesCollection.Add(mySC);

    }
    private SeriesCollection GetDataSource(int j)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        SeriesCollection SC = new SeriesCollection();
        DataSet ds=new DataSet();
        if (j == 0)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "覆冰厚度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 1)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "温度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 2)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "湿度", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 3)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "导线弧垂", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 4)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "风速", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else if (j == 5)
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "导线倾斜角1", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }
        else
        {
            ds = towerDAOs.getSpineB(dropTower.SelectedValue, "悬点导线拉力", RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value);
        }

        int i = ds.Tables[0].Rows.Count;
        if (i != 0)
        {
            if (i > 50)
            {
                i = 50;
            }
            Series s = new Series();
            if (j == 0)
            {
                s.Name = "覆冰厚度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart1.Visible = true;
            }
            else if (j == 1)
            {
                s.Name = "温度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart2.Visible = true;
            }
            else if (j == 2)
            {
                s.Name = "湿度";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart3.Visible = true;
            }
            else if (j == 3)
            {
                s.Name = "导线弧垂";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart4.Visible = true;
            }
            else if (j == 4)
            {
                s.Name = "风速";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart5.Visible = true;
            }
            else if (j == 5)
            {
                s.Name = "导线倾斜角";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart6.Visible = true;
            }
            else if (j == 6)
            {
                s.Name = "导线拉力";
                for (int b = 0; b < i; b++)
                {
                    Element e = new Element();
                    e.Name = Convert.ToDateTime(ds.Tables[0].Rows[b]["time"]).ToString("yyyy-MM-dd");
                    e.YValue = Convert.ToDouble(ds.Tables[0].Rows[b]["value"].ToString());
                    s.Elements.Add(e);
                }
                SC.Add(s);
                this.Chart7.Visible = true;
            }
        }
        return SC;
    }
    protected void dropTower_SelectedIndexChanged(object sender, EventArgs e)
    {
        TowerDAOs towerDAOs = TowerDAOs.getInstance();
        this.txtCJSK.Text = towerDAOs.getNowTime(dropTower.SelectedValue, "温度");
        this.txtWD.Text = towerDAOs.getNow(dropTower.SelectedValue, "温度");
        this.txtSD.Text = towerDAOs.getNow(dropTower.SelectedValue, "湿度");
        this.txtFS.Text = towerDAOs.getNow(dropTower.SelectedValue, "风速");
        this.txtDXQXJ.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线倾斜角1");
        this.txtDXLL.Text = towerDAOs.getNow(dropTower.SelectedValue, "悬点导线拉力");
        this.txtDXHC.Text = towerDAOs.getNow(dropTower.SelectedValue, "导线弧垂");
        this.txtDXLBHD.Text = towerDAOs.getNow(dropTower.SelectedValue, "覆冰厚度");
    }
    protected void btnQX_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 7; i++)
        {
            Drawing(i);
        }
    }
}
